<?php


namespace Admin\Model;
use Think\Model;

class TreememberModel extends Model{

        protected $_validate = array(
            //array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
            array('tel','/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/','手机号格式不正确',0),
            array('pernumber','/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/','身份证号不正确',0),
            array('age','number','年龄不正确',0),
            array('name','require','姓名不能为空',0),
            array('add_1','require','地址不能为空',0),
            array('add_2','require','地址不能为空',0),
            array('add_3','require','地址不能为空',0),
            array('add_4','require','地址不能为空',0),
            array('id','number','ID必须为整数',0)
        );

        /**
         * 更新方法
         */
        public function update()
        {
            $data = $this->create();
            if(!$data){ //数据对象创建错误
                return false;
            }
            /* 添加或更新数据 */
            if(empty($data['id'])){
                $res = $this->add();
            }else{
                $res = $this->save();
            }
            return $res;
        }

        /**
         * 获取会员信息
         */
        public function getinfo($id)
        {
            return $this->field(true)->where('id='.$id)->find();
        }

        /**
         * 验证手机号
         */
        public function check_tel($tel)
        {
            return $this->field(true)->where('tel='.$tel)->find();
        }

        /**
         * 新增会员加分
         * @return boolean fasle 失败
         * @param 推荐人ID
         */
        public function addintegral($uid,$bei)
        {
            $arr_id = D('treestruct')->getpid_arr($uid);
            /**获取6W总账户ID**/
            $agent = D('treestruct')->getagentid($uid);
            $agent_id = $agent['id'];
            M()->startTrans();
            /**6W代理加分**/
            if ($uid != 1 || $agent['lvl'] != null) {
                $agent = $this->where('uid='.$agent_id)->setInc('integral',C('integral_config_6w')*$bei);
                $data = array('uid'=>$agent_id,'type'=>'4','integral'=>'+'.C('integral_config_6w')*$bei,'changetime'=>time(),'changeuid'=>$_SESSION['onethink_admin']['user_auth']['uid']);
                $integral = M('integralchange');
                $res = $integral->data($data)->add();
                if ($res === false) {
                    M()->rollback();
                }
            }
            if ($arr_id === false) {
                $first = $this->where('uid='.$uid)->setInc('integral',C('integral_config_first')*$bei);
                $data = array('uid'=>$uid,'type'=>'4','integral'=>'+'.C('integral_config_first')*$bei,'changetime'=>time(),'changeuid'=>$_SESSION['onethink_admin']['user_auth']['uid']);
                $integral = M('integralchange');
                $res = $integral->data($data)->add();
                if ($res === false) {
                    M()->rollback();
                }
                if ($first === false) {
                    M()->rollback();
                }
                return true;
            } else {
                $first = $this->where('uid='.$arr_id['first'])->setInc('integral',C('integral_config_first')*$bei);
                $data = array('uid'=>$arr_id['first'],'type'=>'4','integral'=>'+'.C('integral_config_first')*$bei,'changetime'=>time(),'changeuid'=>$_SESSION['onethink_admin']['user_auth']['uid']);
                $integral = M('integralchange');
                $res = $integral->data($data)->add();
                if ($res === false) {
                    M()->rollback();
                }
                if ($first === false) {
                    M()->rollback();
                }
                if (!array_key_exists('second', $arr_id)) {
                    return true;
                }
                $second = $this->where('uid='.$arr_id['second'])->setInc('integral',C('integral_config_second')*$bei);
                $data = array('uid'=>$arr_id['second'],'type'=>'4','integral'=>'+'.C('integral_config_second')*$bei,'changetime'=>time(),'changeuid'=>$_SESSION['onethink_admin']['user_auth']['uid']);
                $integral = M('integralchange');
                $res = $integral->data($data)->add();
                if ($res === false) {
                    M()->rollback();
                }
                if ($second === false) {
                    M()->rollback();
                }
                if (!array_key_exists('third', $arr_id)) {
                    return true;
                }
                $third = $this->where('uid='.$arr_id['third'])->setInc('integral',C('integral_config_third')*$bei);
                $data = array('uid'=>$arr_id['third'],'type'=>'4','integral'=>'+'.C('integral_config_third')*$bei,'changetime'=>time(),'changeuid'=>$_SESSION['onethink_admin']['user_auth']['uid']);
                $integral = M('integralchange');
                $res = $integral->data($data)->add();
                if ($res === false) {
                    M()->rollback();
                }
                if ($third === false) {
                    M()->rollback();
                }
                M()->commit();
                return true;
            }
        }

    /**
     * 加分策略
     * @param加分人id,加分数
     */
    public function add_integral($id,$integral)
    {
        $this->where(array('id'=>$id))->setInc('integral',$integral);

    }

}